﻿using DataAccess.DTO;
using DataAccess.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYD.DataAccess;
using XYD.Framework.Data;
using XYD.Framework.Data.Dapper;

namespace DataAccess.DAL
{
    public class PromoterDAL : BaseDAL
    {


       

        public Promoter Get(int id)
        {
            return CONN_TopicSharing.Get<Promoter>(id);
        }

        public bool Insert(Promoter pro)
        {
            return CONN_TopicSharing.Insert<Promoter>(pro);
        }

        public bool Update(Promoter pro)
        {
            return CONN_TopicSharing.Update<Promoter>(pro);
        }


        public bool Delete( int id)
        {
            string sql = " update Promoter set isdel=1  where id=@id ";

            return CONN_TopicSharing.Excute(sql,new {id=id})>0;
        
        }

        public bool Exist(string username)
        {
            return CONN_TopicSharing.SQLQuery<int>(string.Format(" select count(1) from Promoter where isdel=0  and username='{0}' ", username), null).FirstOrDefault() > 0;
        }

        public List<Promoter> ListPage(string strWhere, string orderbys, int pagesize, int pageindex, out int count)
        {
            List<Promoter> list = new List<Promoter>();
            if (string.IsNullOrEmpty(strWhere))
            {
                strWhere = " isdel=0";
            }

            if (string.IsNullOrEmpty(orderbys))
                orderbys = " id ";

            list = CONN_TopicSharing.SQLPager<Promoter>("*", " Promoter ", strWhere, "", "", orderbys, pagesize, pageindex, out count, null).ToList();

            return list;
        }
        /// <summary>
        /// 推广者登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public UserSession LoginSession(string username, string password)
        {
            string sql = " select * from [dbo].[Promoter] where username=@username and password=@password " ;

            Promoter pro = CONN_TopicSharing.Get<Promoter>(sql, new { username = username, password = XYD.Common.EncodeSecurity.EncryptString(password) });
            if (pro != null && pro.IsEnabled )
            {
               //var m= new SysConfigDAL().Get_PromoterId(pro.Id);
               //if (m == null)
               //    m = new SysConfig();
             //  UserSession userSession = new UserSession() { id = Guid.NewGuid().ToString("N"), user_id = pro.Id, user_name = pro.Username, nick_name = pro.Username, time_stamp = XYD.Common.TimeStampHelper.GenerateTimeStamp(DateTime.Now), domain=m.domain };
               UserSession userSession = new UserSession() { id = Guid.NewGuid().ToString("N"), user_id = pro.Id, user_name = pro.Username, nick_name = pro.Username, time_stamp = XYD.Common.TimeStampHelper.GenerateTimeStamp(DateTime.Now)};

                return userSession;
            }

            return null;
        }

        public UserSession GetLoginSession(string sessionid)
        {
            UserSession session = RedisOperation.GetUserSession(sessionid);
            return session;
        }

        public bool SetLoginSession(UserSession session)
        {
            return RedisOperation.SetUserSession(session);
        }


    }
}
